<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;

/**
 * base controller
 */
class BaseController extends Controller
{
    //public $layout = 'main';
    private $noLoginActionList = ['home', 'site', 'rbac', 'test'];

    public function beforeAction($action)
    {
        if (false === parent::beforeAction($action)) {
            return false;
        }

        if($this->isActionNeedLogin()) {
            if(Yii::$app->user->isGuest) {
                $this->goHome();
                return false;
            }
        }

        return true;
    }

    private function isActionNeedLogin() {
        $need_login = false;
        try {
            $path = Yii::$app->request->pathInfo;

            if($path !== null
                    && strlen($path) > 0) {
                $need_login = true;
                $path_split = explode('/', $path);
                if(count($path_split) > 0) {
                    $action = $path_split[0];

                    if($action != null) {
                        if(in_array($action, $this->noLoginActionList)) {
                            $need_login = false;
                        }
                    }
                }
            }
        } catch(\Exception $e) {
        }
        return $need_login;
    }
}
